package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.BrowseRecord;
import com.wuming.house.model.House;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.BrowseRecordService;
import com.wuming.house.service.HouseService;
import com.wuming.house.service.SecondhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/9/16 18:33
 * @Description:
 */
@Controller
@RequestMapping("/web/browseRecord")
public class BrowseRecordController {

    @Autowired
    private BrowseRecordService browseRecordService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private SecondhouseService secondhouseService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Long userid,Integer houseType, @RequestParam(defaultValue = "1")int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize){
        List<House> houses = new ArrayList<>();
        List<Secondhouse> secondhouses = new ArrayList<>();
        try {
            List<BrowseRecord> browseRecords = browseRecordService.getBrowseRecordByPage(userid,houseType,pageIndex,pageSize);
            int total = browseRecordService.getTotal(userid,houseType);
            if (houseType== HouseType.NEW_HOUSE.getValue()){
                for (BrowseRecord browseRecord : browseRecords){
                    House house = houseService.getHouseById(browseRecord.getHouseId());
                    houses.add(house);
                }
                return CommonResult.createSuccessResult(houses,total,"查询新房浏览记录成功");
            }else {
                for (BrowseRecord browseRecord : browseRecords){
                    Secondhouse secondhouse = secondhouseService.getSecondhouseById(browseRecord.getHouseId());
                    secondhouses.add(secondhouse);
                }
                return CommonResult.createSuccessResult(secondhouses,total,"查询二手房浏览记录成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.createFailureResult("查询浏览记录失败");
        }
    }

    @ResponseBody
    @RequestMapping("/edit")
    public JsonResult edit(@RequestParam Long houseId,@RequestParam(defaultValue = "1") Long userid,Integer houseType){
        Date date = new Date();
        try {
            BrowseRecord br = browseRecordService.getBrowseRecordByParams(houseId,userid,houseType);
            if (null==br){
                BrowseRecord browseRecord = new BrowseRecord();
                browseRecord.setHouseId(houseId);
                browseRecord.setUserId(userid);
                browseRecord.setHouseType(houseType);
                browseRecord.setCreateDatetime(date);
                browseRecord.setUpdateDatetime(date);
                browseRecordService.saveBrowseRecord(browseRecord);
                return new JsonResult(null,"保存浏览记录成功",true);
            }else {
                br.setUpdateDatetime(date);
                browseRecordService.modifyBrowseRecord(br);
                return new JsonResult(null,"更新浏览记录成功",true);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(null,"修改浏览记录失败",false);
        }
    }
}
